package com.jdew.thkj.activity;

import android.app.Activity;
import android.app.Dialog;
import android.content.SharedPreferences;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jdew.thkj.R;
import com.jdew.thkj.base.BaseActivity;
import com.jdew.thkj.base.BaseFragment;
import com.jdew.thkj.fragment.FragmentFactory;
import com.jdew.thkj.view.NetworkProgressDialog;

import org.greenrobot.eventbus.EventBus;

import butterknife.Bind;

/**
 * Created by yangzai on 2018/4/20 0020.
 */

public class SortsActivity extends BaseActivity {
    @Bind(R.id.th_viewpager)
    ViewPager viewpager;
    @Bind(R.id.back)
    public ImageView back;
    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.pailie1)
    ImageView pailie1;
    @Bind(R.id.pailie2)
    ImageView pailie2;
    @Bind(R.id.tab)
    TabLayout tab;
    public String sort = "女装";
    public int type = 0;
    public SharedPreferences sp;
    public SharedPreferences.Editor editor;
    public NetworkProgressDialog networkProgressDialog;
    public String recycleType="1";
    public Dialog mWeiboDialog;

    @Override
    public int bindLayout() {
        return R.layout.activity_sorts;
    }

    @Override
    public void initView(View view) {
        back.setVisibility(View.VISIBLE);
        getType();
        pailie1.setOnClickListener(this);
        pailie2.setOnClickListener(this);
        back.setOnClickListener(this);
        sp = getSharedPreferences("user", Activity.MODE_PRIVATE);
        editor = sp.edit();
        ShortPagerAdapter adapter = new ShortPagerAdapter(getSupportFragmentManager());
        viewpager.setAdapter(adapter);
        tab.setupWithViewPager(viewpager);
    }
    public void getType() {
        pailie1.setVisibility(View.GONE);
        pailie2.setVisibility(View.VISIBLE);
        type = getIntent().getIntExtra("type", 0);
        if (type == 0) {
            sort = "1";
            title.setText("女装");
        }else if(type == 1){
            sort = "7";
            title.setText("母婴");
        }else if(type == 2){
            sort = "9";
            title.setText("居家");
        }else if(type == 3){
            sort = "2";
            title.setText("男装");
        }else if(type == 4){
            sort = "8";
            title.setText("鞋包");
        }else if(type == 5){
            sort = "4";
            title.setText("数码");
        }else if(type == 6){
            sort = "5";
            title.setText("美食");
        }else if(type == 7){
            sort = "3";
            title.setText("内衣");
        }else if(type == 8){
            sort = "6";
            title.setText("美妆");
        }else if(type == 9){
            sort = "11";
            title.setText("其它");
        }
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back: {
                finish();
            }
            break;
            case R.id.pailie1: {
                pailie1.setVisibility(View.GONE);
                pailie2.setVisibility(View.VISIBLE);
                EventBus.getDefault().post("1");
                recycleType="1";
            }
            break;
            case R.id.pailie2: {
                pailie1.setVisibility(View.VISIBLE);
                pailie2.setVisibility(View.GONE);
                EventBus.getDefault().post("2");
                recycleType="2";

            }
            break;
            default:
                break;
        }
    }
    private class ShortPagerAdapter extends FragmentPagerAdapter {
        public String[] mTilte=null;

        public ShortPagerAdapter(FragmentManager fm) {
            super(fm);
            mTilte = getResources().getStringArray(R.array.tab_sort_Title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTilte[position];
        }

        @Override
        public BaseFragment getItem(int position) {
            BaseFragment fragment = FragmentFactory.createFragment3(position);
            return fragment;
        }

        @Override
        public int getCount() {
            return mTilte.length;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
